﻿using Newtonsoft.Json;
using PddHelper.Framework;
using System.Net;

namespace PddHelper.Api
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResult<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public ApiStatusCode Code { get; set; } = ApiStatusCode.OK;

        /// <summary>
        /// 结果说明
        /// </summary>
        public string Msg { get; set; } = "SUCCESS";

        /// <summary>
        /// 数据总条数 分页数据使用
        /// </summary>
        public int total { get; set; }

        /// <summary>
        /// 分页每页显示数量
        /// </summary>
        public int pageSize { get; set; }

        /// <summary>
        /// 分页页码
        /// </summary>
        public int pageNumber { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        public dynamic Data { get; set; } 

        /// <summary>
        /// 返回成功ResultCode=2000 用于单个记录
        /// </summary>
        /// <param name="data">需要返回的数据</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public ApiResult<T> Success(dynamic data, string msg = "SUCCESS")
        {
            if (data != null)
            {
                this.Code = ApiStatusCode.OK;
                this.Data = data;
                this.Msg = msg;
            }
            return this;
        }

        /// <summary>
        /// 返回失败(自定义返回)
        /// </summary>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public ApiResult<T> Error(string msg)
        {
            Code = ApiStatusCode.BusinessException;
            Msg = msg;
            return this;
        }
    }
}
